package com.dyzx.hbb.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.sys.entity.SysOrder;
import com.dyzx.hbb.modules.sys.entity.SysWork;
import com.dyzx.hbb.modules.sys.query.WorkQuery;
import com.dyzx.hbb.modules.sys.service.SysWorkService;
import com.dyzx.hbb.modules.sys.vo.WorkProcessVO;
import com.dyzx.hbb.modules.sys.vo.WorkUVO;
import com.dyzx.hbb.modules.sys.vo.WorkVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/sys/work")
@RestController
@Tag(name = "12.工单管理", description = "工单相关接口")
public class WorkController extends BaseController<SysWorkService, SysWork, WorkQuery, WorkVO, WorkUVO> {

    @Autowired
    private SysWorkService service;


    @Override
    protected SysWorkService getBaseService() {
        return service;
    }


    @Override
    protected MyLambdaQueryWrapper<SysWork> queryWrapper(WorkQuery query) {
        MyLambdaQueryWrapper<SysWork> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysWork::getId, SysWork::getTitle, SysWork::getCategory, SysWork::getLevel, SysWork::getStatus,
                SysWork::getCreateDate, SysWork::getUpdateDate,SysWork::getStatus).userName(SysWork::getOperatorId).as("operator_name").
                select(SysWork::getProcessUserId).as("process_user_id").select(SysWork::getProcessDate).as("process_date")
                .userName(SysWork::getProcessUserId).as("process_user_name").imageUrls(SysWork::getImage).as("image_url").organName(SysWork::getOrganId).as("organ_name");
        if (query != null) {


            wrapper.like(StringUtils.isNotBlank(query.getTitle()), SysWork::getTitle, query.getTitle())
                    .eq(query.getStatus()!=null, SysWork::getStatus, query.getStatus())
                    .eq(query.getCategory()!=null, SysWork::getCategory, query.getCategory())
                    .dateBetween(StringUtils.isNotBlank(query.getCreateDate()), SysWork::getCreateDate, DateFormat.DATE, query.getCreateDate())
                    .eq(query.getLevel()!=null, SysWork::getLevel, query.getLevel()).
                    eq(query.getOrganId()!=null, SysWork::getOrganId, query.getOrganId());

        }
        return wrapper;

    }

    @Override
    protected MyLambdaQueryWrapper<SysWork> queryInfoWrapper() {
        MyLambdaQueryWrapper<SysWork> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysWork::getId, SysWork::getTitle, SysWork::getCategory, SysWork::getLevel, SysWork::getStatus, SysWork::getCreateDate,SysWork::getDescription, SysWork::getUpdateDate, SysWork::getOperatorId, SysWork::getOrganId).organName(SysWork::getOrganId).as("organ_name");

        wrapper.userName(SysWork::getOperatorId).as("operator_name").
                select(SysWork::getProcessUserId).as("process_user_id")
                .select(SysWork::getProcessDate).as("process_date")
                .select(SysWork::getProcessNote).as("process_note")
                .userName(SysWork::getProcessUserId).as("process_user_name");
        return wrapper;
    }

    //工单处理 PUT /sys/work/process
    @Operation(summary = "工单处理")
    @PutMapping("/process")
    public Result<Boolean> process(@RequestBody WorkProcessVO vo) {
        Long userId= SecurityContextHolder.getUserId();
        SysWork work = service.getById(vo.getId());
        if (work == null) {
            return Result.error("工单不存在");
        }
        work.setProcessUserId(userId);
       work.setProcessDate(System.currentTimeMillis());
        BeanUtil.copyProperties( vo,work);
        return service.updateById(work) ? Result.ok() : Result.error();

    }

}
